﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTOSingTowerMall;
using FacadeSingTowerMall;
using System.Text;

namespace WebFormSingTowerMall
{
    public partial class StoreChart : System.Web.UI.Page
    {
        VWStoreFacade store = new VWStoreFacade();
        StringBuilder str = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                chart_bind();
            }
        }

        string GenerateStoreByLevelChartString(int Level)
        {
            //Level = 0, means generate for all stores in all levels
            string Title;

            if (Level == 0) Title = "All Level Stores Categories";
            else Title = "Level " + Level + " Stores Categories";

            try
            {
                var stores = from s in store.StoreByLevel(Level) select s;
                if (stores.Count() > 0)
                {
                    // Create the strings that will draw the chart
                    str.Append(@"<script type=text/javascript>  google.load('visualization', '1', {'packages':['corechart']});
                     google.setOnLoadCallback(drawChart);
                     function drawChart() {
                     var data = new google.visualization.DataTable();");

                    str.Append(" data.addColumn('string', 'Category_Name');");
                    str.Append(" data.addColumn('number', 'Count');");
                    str.Append(" data.addRows([");
                    foreach (VWStoreGroup st in stores)
                    {
                        str.Append("['" + (st.Name.ToString()) + "'," + st.Count.ToString() + "],");
                    }

                    str.Append(" ]);");
                    str.Append(" var chart = new google.visualization.PieChart(document.getElementById('chart_div" + Level.ToString() + "'));");
                    //str.Append(" var chart = new google.visualization.BarChart(document.getElementById('chart_div'));");
                    str.Append("chart.draw(data, {width: 400, height: 400,title: '" + Title + "'});}");
                    str.Append("</script>");
                }
                else str = null;
            }
            catch(Exception e){ throw new Exception(e.Message); }
            return str.ToString().TrimEnd(',');
        }

        private void chart_bind()
        {
            // here i am using SqlDataAdapter for the sql server select query
            try
            {
                // here am using literal conrol to display the complete graph
                lt.Text += GenerateStoreByLevelChartString(0) + Environment.NewLine;
                lt.Text += GenerateStoreByLevelChartString(1) + Environment.NewLine;
                lt.Text += GenerateStoreByLevelChartString(2) + Environment.NewLine;

                /*
                Chart1.DataSource = from s in store.StoreByLevel(0) select s;
                Chart1.Series["Series1"].XValueMember = "Name";
                Chart1.Series["Series1"].YValueMembers = "Count";
                Chart1.DataBind();
                 * */
            }
            catch { }
        }

    }
}